using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;

namespace IGlobalist.Cms.Common
{
    public class PaymentPro
    {
        private static string paymentProFilePath = "Admin\\PaymentPro.config";
        private static XmlDocument xDoc;
      
        static PaymentPro()
        {
             paymentProFilePath = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, paymentProFilePath);
             
        }

        public static void SetProfilePaypalInfo(){
            PaymentInfo payment = GetMerchantAccount(PaymentSystem.Paypal);
            SetProfilePaypal.SessionProfile = SetProfilePaypal.CreateAPIProfile(payment.APIUsername, payment.APIPassword, payment.Signature,payment.Environment, "", "", "", ""); 
        }

        public static PaymentInfo GetProfilePaypalInfo()
        {
            PaymentInfo payment = GetMerchantAccount(PaymentSystem.Paypal);
            return payment;
        }

        public static PaymentInfo GetProfileGogleCheckOut()
        {
            PaymentInfo payment = GetMerchantAccount(PaymentSystem.GoogleCheckout);
            return payment;
        }

        public static PaymentInfo GetMerchantAccount(PaymentSystem paymentType)
        {
            try
            {
                PaymentInfo paymentInfo = new PaymentInfo();
                LoadXML();

                if (paymentType == PaymentSystem.Paypal)
                {
                    string xpath = String.Format("//payment[@type='{0}']", GetPaymentType(paymentInfo.PaymentSystem));
                    XmlNode xNode = xDoc.DocumentElement.SelectSingleNode(xpath);

                    paymentInfo.APIUsername = xNode.SelectSingleNode("Info[@key='username']").Attributes["value"].Value;
                    paymentInfo.APIPassword = xNode.SelectSingleNode("Info[@key='password']").Attributes["value"].Value;
                    paymentInfo.Signature = xNode.SelectSingleNode("Info[@key='signature']").Attributes["value"].Value;
                    paymentInfo.Environment = xNode.SelectSingleNode("Info[@key='enviroment']").Attributes["value"].Value;
                    paymentInfo.PaymentSystem = paymentType;
                    return paymentInfo;
                }

                if (paymentType == PaymentSystem.GoogleCheckout)
                {
                    string xpath = String.Format("//payment[@type='{0}']", "googlecheckout");
                    XmlNode xNode = xDoc.DocumentElement.SelectSingleNode(xpath);

                    paymentInfo.RootDataFolder = xNode.SelectSingleNode("Info[@key='RootDataFolder']").Attributes["value"].Value;
                    paymentInfo.GoogleMerchantID = xNode.SelectSingleNode("Info[@key='GoogleMerchantID']").Attributes["value"].Value;
                    paymentInfo.GoogleMerchantKey = xNode.SelectSingleNode("Info[@key='GoogleMerchantKey']").Attributes["value"].Value;
                    paymentInfo.GoogleEnvironment = xNode.SelectSingleNode("Info[@key='GoogleEnvironment']").Attributes["value"].Value;
                    paymentInfo.PaymentSystem = paymentType;
                    return paymentInfo;
                }

                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void UpdateMerchantAccount(PaymentInfo paymentInfo)
        {
            try
            {
                  LoadXML();
                  if (paymentInfo.PaymentSystem == PaymentSystem.Paypal)
                  {
                      string xpath = String.Format("//payment[@type='{0}']", GetPaymentType(paymentInfo.PaymentSystem));
                      XmlNode xNode = xDoc.DocumentElement.SelectSingleNode(xpath);
                      xNode.SelectSingleNode("Info[@key='username']").Attributes["value"].Value = paymentInfo.APIUsername;
                      xNode.SelectSingleNode("Info[@key='password']").Attributes["value"].Value = paymentInfo.APIPassword;
                      xNode.SelectSingleNode("Info[@key='signature']").Attributes["value"].Value = paymentInfo.Signature;
                      xNode.SelectSingleNode("Info[@key='enviroment']").Attributes["value"].Value = paymentInfo.Environment;
                  }

                  if (paymentInfo.PaymentSystem == PaymentSystem.GoogleCheckout)
                  {
                      string xpath = String.Format("//payment[@type='{0}']", GetPaymentType(paymentInfo.PaymentSystem));
                      XmlNode xNode = xDoc.DocumentElement.SelectSingleNode(xpath);
                      xNode.SelectSingleNode("Info[@key='RootDataFolder']").Attributes["value"].Value = paymentInfo.RootDataFolder;
                      xNode.SelectSingleNode("Info[@key='GoogleMerchantID']").Attributes["value"].Value = paymentInfo.GoogleMerchantID;
                      xNode.SelectSingleNode("Info[@key='GoogleMerchantKey']").Attributes["value"].Value = paymentInfo.GoogleMerchantKey;
                      xNode.SelectSingleNode("Info[@key='GoogleEnvironment']").Attributes["value"].Value = paymentInfo.GoogleEnvironment;
                  }

              Save();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private static string GetPaymentType(PaymentSystem payment)
        {
            string type = "";
            switch (payment)
            {
                case PaymentSystem.Paypal:
                    type = "paypal";
                    break;
                case PaymentSystem.GoogleCheckout:
                    type = "googlecheckout";
                    break;
            }
            return type;
        }

        

        
        public static void Save()
        {
            lock (xDoc)
            {
                xDoc.Save(paymentProFilePath);
                SetProfilePaypalInfo();
            }
        }

         #region Load Config
         private static void LoadXML()
         {
             if (xDoc != null) return;
             xDoc = new XmlDocument();
             lock (xDoc)
             {
                 xDoc.Load(paymentProFilePath);
                 try
                 {
                     HttpContext.Current.Cache.Add("Admin/PaymentPro.config", xDoc, new System.Web.Caching.CacheDependency(paymentProFilePath), DateTime.Now.AddYears(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, new System.Web.Caching.CacheItemRemovedCallback(CacheRemoveCallback));
                 }
                 catch 
                 {
                     //throw e;
                 }
             }
         }

         private static void CacheRemoveCallback(string key, object obj, System.Web.Caching.CacheItemRemovedReason reason)
         {
             xDoc = null;
             LoadXML();
         }
        #endregion
     }


    public class PaymentInfo
    {
        public PaymentInfo()
        {

        }
        private string _APIUsername;
        public string APIUsername
        {
            get { return _APIUsername; }
            set { _APIUsername = value; }
        }

        private string _APIPassword;
        public string APIPassword
        {
            get { return _APIPassword; }
            set { _APIPassword = value; }
        }

        private string _Signature;
        public string Signature
        {
            get { return _Signature; }
            set { _Signature = value; }
        }


        private string _RootDataFolder;
        public string RootDataFolder
        {
            get { return _RootDataFolder; }
            set { _RootDataFolder = value; }
        }

        private string _GoogleMerchantID;
        public string GoogleMerchantID
        {
            get { return _GoogleMerchantID; }
            set { _GoogleMerchantID = value; }
        }

        private string _GoogleMerchantKey;
        public string GoogleMerchantKey
        {
            get { return _GoogleMerchantKey; }
            set { _GoogleMerchantKey = value; }
        }

        private string _GoogleEnvironment;
        public string GoogleEnvironment
        {
            get { return _GoogleEnvironment; }
            set { _GoogleEnvironment = value; }
        }

        private PaymentSystem _PaymentSystem = PaymentSystem.Paypal;
        public PaymentSystem PaymentSystem
        {
            get { return _PaymentSystem; }
            set { _PaymentSystem = value; }
        }

        private string _Environment;
        public string Environment
        {
            get { return _Environment; }
            set { _Environment = value; }
        }

    }
}
